package com.lebang.medicalInquiry.controller;


import com.lebang.medicalInquiry.constant.ResponseEnum;
import com.lebang.medicalInquiry.model.po.Organization;

import com.lebang.medicalInquiry.model.po.User;
import com.lebang.medicalInquiry.model.vo.OrganizationOperationListVo;
import com.lebang.medicalInquiry.model.vo.PageVo;
import com.lebang.medicalInquiry.model.vo.admin.OrganizationListVo;
import com.lebang.medicalInquiry.model.vo.admin.PhysicianListVo;
import com.lebang.medicalInquiry.model.vo.admin.PhysicianOrganizationReVo;
import com.lebang.medicalInquiry.response.Callback;
import com.lebang.medicalInquiry.response.JsonData;
import com.lebang.medicalInquiry.service.OrganizationService;
import com.lebang.medicalInquiry.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

@RequestMapping("/organization")
@Api(tags = "机构相关")
@RestController
@Validated
public class OrganizationController {


    @Autowired
    private OrganizationService organizationService;

    @GetMapping("/")
    @ApiOperation("机构列表")
    public JsonData<PageVo<List<OrganizationListVo>>> listArticle(HttpServletRequest request, HttpServletResponse response,
                                                                  @ApiParam(name = "keyWord", value = "标题关键词") @RequestParam(value = "keyWord", required = false) String keyWord,
                                                                  @ApiParam(name = "state", value = "0 禁用 1 启用 -1 所有") @RequestParam(value = "state", required = false, defaultValue = "-1") Integer state,
                                                                  @ApiParam(name = "page", value = "页码") @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                  @ApiParam(name = "limit", value = "条数") @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

        PageVo<List<OrganizationListVo>> list = organizationService.getList(keyWord, state, page, limit);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), list);

    }

    @PostMapping("/")
    @ApiOperation("修改机构")
    public JsonData operator(HttpServletRequest request, HttpServletResponse response
            , @RequestBody Organization po

    ) {
        organizationService.operator(po);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());

    }

    @DeleteMapping("/{organizationId}")
    @ApiOperation("删除机构")
    public JsonData operator(HttpServletRequest request, HttpServletResponse response,
                             @ApiParam(name = "organizationId", value = "机构id") @PathVariable(value = "organizationId") Integer organizationId

    ) {
        organizationService.remove(organizationId);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());

    }


    @GetMapping("/operator")
    @ApiOperation("机构运营人员列表")
    public JsonData<PageVo<List<OrganizationOperationListVo>>> getOperaList(HttpServletRequest request, HttpServletResponse response,
                                                                            @ApiParam(name = "keyWord", value = "标题关键词") @RequestParam(value = "keyWord", required = false) String keyWord,
                                                                            @ApiParam(name = "organizationId", value = "机构id") @RequestParam(value = "organizationId", required = false) Integer organizationId,
                                                                            @ApiParam(name = "state", value = "0 禁用 1 启用 -1 所有") @RequestParam(value = "state", required = false, defaultValue = "-1") Integer state,
                                                                            @ApiParam(name = "page", value = "页码") @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                            @ApiParam(name = "limit", value = "条数") @RequestParam(value = "limit", defaultValue = "10") Integer limit) {


        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), organizationService.getOperaList(organizationId, keyWord, state, page, limit));

    }

    @PostMapping("/operator")
    @ApiOperation("修改机构运营人员")
    public JsonData operatorOpera(HttpServletRequest request, HttpServletResponse response
            , @RequestBody OrganizationOperationListVo
                                          organizationOperation
    ) {
        organizationService.operatorOpera(organizationOperation);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());

    }

    @DeleteMapping("/operator/{opeartorId}")
    @ApiOperation("删除机构运营人员")
    public JsonData removeOpera(HttpServletRequest request, HttpServletResponse response,
                                @ApiParam(name = "opeartorId", value = "运营人员") @PathVariable(value = "opeartorId") Integer opeartorId

    ) {
        organizationService.removeOpera(opeartorId);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());

    }


    @PostMapping("/allocation/physician/{opeartorId}")
    @ApiOperation("给机构运营人员分配对应的医生")
    public JsonData allocationPhysician(HttpServletRequest request, HttpServletResponse response,
                                        @ApiParam(name = "opeartorId", value = "运营人员") @PathVariable(value = "opeartorId") Integer opeartorId,
                                        @ApiParam(name = "physicianIds", value = "多个医生id") @RequestParam(value = "physicianIds", required = false) String physicianIds

    ) {
        organizationService.allocationPhysician(physicianIds, opeartorId);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());

    }

    @GetMapping("/physician")
    @ApiOperation("查询机构下的医师")
    public JsonData getAllPhysician(HttpServletRequest request, HttpServletResponse response

    ) {
        List<PhysicianOrganizationReVo> physicianListVos = organizationService.getAllPhysician();

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), physicianListVos);

    }
}